# Copyright 2020 The StackStorm Authors.
# Copyright 2019 Extreme Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import

from oslo_config import cfg

import st2common.config as common_config
from st2common.constants.system import VERSION_STRING
from st2common.constants.system import DEFAULT_CONFIG_FILE_PATH
from st2common.constants.garbage_collection import DEFAULT_COLLECTION_INTERVAL
from st2common.constants.garbage_collection import DEFAULT_SLEEP_DELAY

CONF = cfg.CONF


def parse_args(args=None):
    common_config.use_st2_env_vars(cfg.CONF)
    cfg.CONF(
        args=args,
        version=VERSION_STRING,
        default_config_files=[DEFAULT_CONFIG_FILE_PATH],
    )


def register_opts(ignore_errors=False):
    _register_common_opts(ignore_errors=ignore_errors)
    _register_garbage_collector_opts(ignore_errors=ignore_errors)


def get_logging_config_path():
    return cfg.CONF.garbagecollector.logging


def _register_common_opts(ignore_errors=False):
    common_config.register_opts(ignore_errors=ignore_errors)


def _register_garbage_collector_opts(ignore_errors=False):
    logging_opts = [
        cfg.StrOpt(
            "logging",
            default="/etc/st2/logging.garbagecollector.conf",
            help="Location of the logging configuration file.",
        )
    ]

    common_config.do_register_opts(
        logging_opts, group="garbagecollector", ignore_errors=ignore_errors
    )

    common_opts = [
        cfg.IntOpt(
            "collection_interval",
            default=DEFAULT_COLLECTION_INTERVAL,
            help="How often to check database for old data and perform garbage collection.",
        ),
        cfg.FloatOpt(
            "sleep_delay",
            default=DEFAULT_SLEEP_DELAY,
            help="How long to wait / sleep (in seconds) between "
            "collection of different object types.",
        ),
    ]

    common_config.do_register_opts(
        common_opts, group="garbagecollector", ignore_errors=ignore_errors
    )

    ttl_opts = [
        cfg.IntOpt(
            "action_executions_ttl",
            default=None,
            help="Action executions and related objects (live actions, action output "
            "objects) older than this value (days) will be automatically deleted. Defaults to None (disabled).",
        ),
        cfg.IntOpt(
            "action_executions_output_ttl",
            default=7,
            help="Action execution output objects (ones generated by action output "
            "streaming) older than this value (days) will be automatically deleted. Defaults to 7.",
        ),
        cfg.IntOpt(
            "trigger_instances_ttl",
            default=None,
            help="Trigger instances older than this value (days) will be automatically deleted. Defaults to None (disabled).",
        ),
        cfg.IntOpt(
            "rule_enforcements_ttl",
            default=None,
            help="Rule enforcements older than this value (days) will be automatically deleted. Defaults to None (disabled).",
        ),
        cfg.IntOpt(
            "tokens_ttl",
            default=None,
            help="Tokens that expired over this value (days) will be automatically deleted. Defaults to None (disabled).",
        ),
        cfg.IntOpt(
            "traces_ttl",
            default=None,
            help="Trace objects older than this value (days) will be automatically deleted. Defaults to None (disabled).",
        ),
        cfg.IntOpt(
            "workflow_executions_ttl",
            default=None,
            help="Workflow execution output objects (generated by action output "
            "streaming) older than this value (days) will be automatically deleted. "
            "Defaults to None (disabled).",
        ),
        cfg.IntOpt(
            "task_executions_ttl",
            default=None,
            help="Workflow task execution output objects (generated by action output "
            "streaming) older than this value (days) will be automatically deleted. "
            "Defaults to None (disabled).",
        ),
    ]

    common_config.do_register_opts(
        ttl_opts, group="garbagecollector", ignore_errors=ignore_errors
    )

    inquiry_opts = [
        cfg.BoolOpt(
            "purge_inquiries",
            default=False,
            help="Set to True to perform garbage collection on Inquiries (based on "
            "the TTL value per Inquiry)",
        )
    ]

    common_config.do_register_opts(
        inquiry_opts, group="garbagecollector", ignore_errors=ignore_errors
    )


register_opts(ignore_errors=True)
